﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using Principia.FormalSystem.Parser;
using Principia.FormalSystem.Reasoning;
using NUnit.Framework;
using Principia.FormalSystem;
using NUnit.Framework.SyntaxHelpers;

namespace Principia.UnitTests.FormalSystemTestSuite
{
    public static class Check
    {
        public static void Follows<T>(this string Goal, string Basis, params string[] Premises)
            where T : Justification
        {
            DoCheck(Basis, Goal, Premises, (j, desc) =>
            {
                Assert.That(j, Is.Not.Null, "Could not reason that {0}", desc);
                Assert.That(j as T, Is.Not.Null, "Unexpectedly reasoned that {0} by means of {1}",
                            desc, j.GetType());

                Console.WriteLine("Could reason that {0}", desc);
            });
        }

        public static void NotFollows(this string Goal, string Basis, params string[] Premises)
        {
            DoCheck(Basis, Goal, Premises, (j, desc) =>
                                               {
                                                   Assert.That(j, Is.Null, "Unexpectedly could reason that {0}", desc);

                                                   Console.WriteLine("Could not reason that {0}", desc);
                                               });
        }

        private static void DoCheck(string Basis, string Goal, IEnumerable<string> Premises, Action<Justification,string> Check)
        {
            var b = Basis.Parse();
            var g = Goal.Parse();
            var p = (from P in Premises select P.Parse()).ToArray();

            var desc = string.Format("{0} follows from {1}", g, b);

            Check(g.Follows(b, p), desc);
        }
    }
}